isperp.createTreeSelector = function(options) {
	var o = $.extend({}, {
		id : "",
		parentIdControlId : undefined,
		parentIdDesc : undefined,
		treeUrl : undefined,
		tree : undefined,
		valueControlId : "",
		displayControlId : "",
		displayColumnName : "name",
		autocompleteUrl : undefined,
		afterSelected : undefined,
		valueColumn : undefined,
		displayColumn : undefined,
		cusDialogWidth : 400,
		cusDialogHeight : 380,
		onlySelectLeaf : false
	}, options);

	var self = this, $element = $(this), init = function() {
		$result.tree = o.tree;

		$result.dialog = $("#" + o.id + "Dialog").dialog({
			width : o.cusDialogWidth,
			height : o.cusDialogHeight,
			modal : true,
			autoOpen : false,
			buttons : [ {
				text : "确定",
				"class" : 'nui-mainbtn',
				click : function() {
					var me = this;
					var nodes = $result.tree.zTree.getSelectedNodes();

					if (nodes != undefined && nodes != null && nodes.length == 1) {
						$result.selectNode(nodes[0]);
					}

				}
			}, {
				text : "关闭",
				click : function() {
					$(this).dialog("close");
				}
			} ]
		});

		if (o.autocompleteUrl) {
			$("#" + o.id).autocomplete({
				width : o.cusDialogWidth,
				source : function(request, response) {
					$.ajax({
						url : o.autocompleteUrl,
						dataType : "json",
						data : {
							term : request.term
						},
						success : function(data) {
							response($.map(data.content, function(item) {
								return {
									data : item,
									value : item[o.displayColumn]
								}
							}));
						}
					});
				},
				minLength : 1,
				change : function(event, ui) {
					if (!ui.item) {
						if (o.afterSelectEmpty) {
							o.afterSelectEmpty(event, ui);
						}
					}
				},
				select : function(event, ui) {
					var ids = ui.item.data[o.valueColumn];
					var names = ui.item.data[o.displayColumn];
					var r = true;
					if (o.afterSelected) {
						r = o.afterSelected(ids, names, ui.item.data);
					}
					if (r == false) {

						$("#" + o.valueControlId).val("");
						$("#" + o.id).val("");

						return false;
					} else {
						$("#" + o.valueControlId).val(ids);
						$("#" + o.id).val(names);
					}

				}
			}).data("ui-autocomplete")._renderItem = function(ul, item) {
				return $("<li><a href='javascript:void(0);' data-vid='" + item.data[o.valueColumn] + "'>" + item.data[o.displayColumn] + "</a></li>").appendTo(ul);
			};
		}

	}, $result = {
		tree : null,
		dialog : null,
		selectNode : function(node) {

			if (o.onlySelectLeaf) {
				if (!node.leaf) {
					alert("请选择叶子节点！");
					return;
				}
			}
			if (o.afterSelected) {
				var v = o.afterSelected(node[o.valueColumn], node[o.displayColumn], node);
				if (v == false) {
					return v;
				}
			}
			$("#" + o.valueControlId).val(node[o.valueColumn]);
			$("#" + o.displayControlId).val(node[o.displayColumn]);
			$result.dialog.dialog("close");
		},
		onNodeDblClick : function(event, treeId, treeNode) {
			if (treeNode.leaf) {
				$result.selectNode(treeNode);
			}
		},
		openSelectDialog : function() {

			if (o.parentIdControlId) {
				var pVal = $("#" + o.parentIdControlId).val();

				if (pVal) {
					$("#" + o.id + "Dialog").dialog("open");
					if ($result.tree.zTree.setting.async.url) {
						if (pVal != $result.parentIdControlIdVal) {
							$result.tree.zTree.setting.async.url = o.treeUrl + "?parentId=" + pVal;
							$result.tree.reload();
						}
					} else {
						$result.tree.zTree.setting.async.url = o.treeUrl + "?parentId=" + pVal;
						$result.tree.reload();
					}

					$result.parentIdControlIdVal = pVal;
				} else {
					isperp.alert(o.parentIdDesc ? "请选择" + o.parentIdDesc : "请选择分类");
					return false;
				}
			} else {
				$("#" + o.id + "Dialog").dialog("open");
				if (!$result.tree.zTree.setting.async.url) {

					$result.tree.zTree.setting.async.url = o.treeUrl;
					$result.tree.reload();
				}
			}
			return false;
		}

	};
	init();
	return $result;
};
